export const createBuildConfig = (env: Record<string, string>): { [key: string]: any } => {
  const { NODE_ENV: nodeEnv } = env;
  const isProduction = nodeEnv === 'production';
  return {
    minify: 'esbuild', // 压缩
    sourcemap: !isProduction, // 这个生产环境一定要关闭，不然打包的产物会很大
    reportCompressedSize: false, // 关闭文件计算
    cssCodeSplit: true,
    chunkSizeWarningLimit: 300, // 降低警告阈值
    rollupOptions: {
      output: {
        chunkFileNames: 'js/[name]-[hash].js',
        entryFileNames: 'js/[name]-[hash].js',
        assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
        manualChunks(id: string) {
          if (!id.includes('node_modules')) return undefined;

          // 将核心框架分开，提高缓存效率
          if (id.includes('/vue')) return 'vue-core';
          if (id.includes('vue-router') || id.includes('pinia')) return 'vue-ecosystem';

          // UI库分离到更小的chunks
          if (id.includes('hive-plus') || id.includes('@element-plus') || id.includes('element-plus')) {
            // 将UI库进一步分离
            if (id.includes('theme-chalk') || id.includes('theme')) return 'ui-theme';
            if (id.includes('components')) return 'ui-components';
            return 'ui-core';
          }

          // 图表和可视化库
          if (id.includes('echarts')) return 'charts-vendor';

          // 3D相关库
          if (id.includes('/three')) return 'three-vendor';

          // 编辑器相关
          if (id.includes('@wangeditor') || id.includes('@toast-ui') || id.includes('codemirror')) return 'editor-vendor';

          // 日历组件
          if (id.includes('@fullcalendar')) return 'calendar-vendor';

          // 地图相关
          if (id.includes('vjmap')) return 'vjmap-vendor';

          // 动画库
          if (id.includes('gsap')) return 'animation-vendor';

          // 工具库
          if (id.includes('dayjs') || id.includes('axios') || id.includes('lodash') || id.includes('qs')) return 'utils-vendor';

          // 小的依赖可以合并到一个vendor chunk
          const smallLibs = [
            'animate.css', 'driver.js', 'cropperjs', 'css-color-function',
            'path-browserify', 'sortablejs', '@vueuse/core'
          ];

          if (smallLibs.some(lib => id.includes(lib))) return 'misc-vendor';

          // Fallback: 按包名分组，但避免创建过大的chunks
          const pkgName = id.toString().split('node_modules/')[1]?.split('/')[0];
          if (pkgName) {
            // 对于大型库，单独创建chunk
            if (pkgName.startsWith('@')) {
              // scoped packages，取前两段
              const scopedName = pkgName.split('/').slice(0, 2).join('/');
              return scopedName.replace('@', '').replace('/', '-');
            }
            return pkgName;
          }

          return undefined;
        },
        format: 'es',
      },
    },
    terserOptions: {
      compress: {
        // 生产环境时移除console
        drop_console: isProduction,
        drop_debugger: isProduction,
        // 启用更多压缩选项
        pure_funcs: isProduction ? ['console.log', 'console.info', 'console.debug', 'console.warn'] : [],
      },
    },
  };
};
